import java.io.File;
import java.io.FileOutputStream;

/*
 * 0     0-15
 * 30   15-40
 * 50   40-60
 * 70   60-75
 * 80   75-85
 * 90   85-95
 * 100  95-100
 * 
 */

public class COMPLICATED_HISTOGRAM extends Scheduler implements Runnable {
	static final byte GLOBAL_MAX_COUNT = 100;
	int start;
	int stop;
	byte[][] BLOCK;
	static int numberOfBins = 2;

	public COMPLICATED_HISTOGRAM(int start, int stop) {
		this.start = start;
		this.stop = stop;
	}

	void writeBlock() {
		for (int n = 0; n < numberOfBins; n++) {
			String FILE_NAME = BLOCK_PATH + n + "/" + start + ".bin";
			File out = new File(FILE_NAME);
			FileOutputStream fos;
			try {
				fos = new FileOutputStream(out);
				fos.write(BLOCK[n]);
			} catch (Exception e) {
				e.printStackTrace();
			}

			System.out.println("Done writing: " + FILE_NAME + " @ "
					+ this.getClass());
		}

	}

	@Override
	public void run() {
		int ilen = USERB[0].ilen;
		// b1 b2
		// .  .
		// .  . //Bin1UserVote

		BLOCK = new byte[numberOfBins][(stop - start) * ilen];
		int c = 0;
		for (int bUserID = start; bUserID < stop; bUserID++) {
			for (int i = 0; i < ilen; i++) {
				for (int bin = 0; bin < numberOfBins; bin++) {

					User aUser = USERA[bUserID];
					byte[] votes = aUser.votes;

					for (byte vote : votes) {
						if (vote < 15) {
							BLOCK[bin][c * ilen + i]++;
						} else if (vote < 40) {
							BLOCK[bin][c * ilen + i]++;
						}
					}
				}
			}
			c++;
		}

		writeBlock();
	}

	void normalize(int[] count) {
		byte[] norm = new byte[count.length];

		for (int p = 0; p < count.length; p++) {
			if (norm[p] > 50) { // x / 100 = 100 // 10000
				int countOverGlobalMax = count[p] / GLOBAL_MAX_COUNT;
				if (countOverGlobalMax > 100) {
					countOverGlobalMax = 100;
				}
				norm[p] = (byte) countOverGlobalMax;
			} else {
				norm[p] = (byte) count[p];
			}
		}
	}

	void predict(int bUserID, int bItemID) {
		int[] count = new int[numberOfBins];

		User aUser = USERA[bUserID];
		byte[] votes = aUser.votes;

		for (byte vote : votes) {
			if (vote < 15) {
				count[0]++;
			} else if (vote < 40) {
				count[1]++;
			}
		}
	}
}